//
//  Task_GetNewsDetail.m
//  iphone.mobileoa
//
//  Created by 李林 on 13-10-24.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//  请求获取新闻详情

#import "Task_GetInfoDetail.h"
#import "NewsAttachModel.h"

@implementation Task_GetInfoDetail
-(void)execute
{

    NSString *url           = NSLocalizedString(@"Epoint_DNet_WMH", @"");
    NSString *ValidateData  = [EpointBizTools getToken];
    NSString *namespace     = NSLocalizedString(@"Epoint_DNet_WMH_NameSpace", @"");
    
    NSString *InfoID    = [params objectForKey:@"InfoID"];
    NSString *CateNum   = [params objectForKey:@"CateNum"];
    NSString *IsNeedUrl = [params objectForKey:@"IsNeedUrl"];
    NSString *imgwidth  = [params objectForKey:@"imgwidth"];
    NSString *isvideo   = [params objectForKey:@"isvideo"];
    
    //请求接口
    NSString *method = @"GetInfoDetail";
    NSString *ParasXml = @"<![CDATA[<?xml version=\"1.0\" encoding=\"gb2312\"?><paras><InfoID>%@</InfoID><CateNum>%@</CateNum><IsNeedUrl>%@</IsNeedUrl><isvideo>%@</isvideo><imgwidth>%@</imgwidth></paras>]]>";
    ParasXml = [NSString stringWithFormat:ParasXml,InfoID,CateNum,IsNeedUrl,isvideo,imgwidth];
    
    NSString *key = InfoID;
    
    EpointWebServiceUtil *ws = [[EpointWebServiceUtil alloc]initWithUrl:url namespace:namespace method:method];
    [ws addParameterForString:@"ValidateData" value:ValidateData];
    [ws addParameterForString:@"ParasXml" value:ParasXml];
    NSString *bs =  ws.start;
    
    NSMutableArray *result = [self validateXMLWS2:bs];
    NSString *msg = [result objectAtIndex:1];
    if ([[result objectAtIndex:0]isEqualToString:@"1"]) {
        [DBFrameService setCacheValue:key value:bs];
        [self exeSuccessWithData:bs];
    }
    else
    {
        [self userCache:key message:msg];
    }
}

-(void)userCache:(NSString *)key message:(NSString *)msg
{
    //获取缓存数据
    NSString *cacheBS = [DBFrameService getCacheValue:key];
    
    //判断是否存在缓存
    if (cacheBS.length>0) {
        [self exeSuccessWithData:cacheBS];
    }
    else
    {
        [self exeFailureWithMessage:msg];
    }
}

#pragma mark 解析XML
+(NSMutableArray *) getAttachLists:(NSString *)result
{
    NSString *UserArea = [EpointStringUtil getXMLFormatedAttributeBsWithTag:result xmlTag:@"attachfiles"];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:UserArea  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"SingleFile"];
    NSMutableArray *dataList = [[NSMutableArray alloc]init];
    for (GDataXMLElement *arr in arrays) {
        
        NewsAttachModel *f = [[NewsAttachModel alloc]init];
        
        GDataXMLElement *AttFileName = [[arr elementsForName:@"AttFileName"] objectAtIndex:0];
        f.AttFileName = [AttFileName stringValue];
        
        GDataXMLElement *url = [[arr elementsForName:@"url"] objectAtIndex:0];
        f.url = [url stringValue];
        
        [dataList addObject:f];
    }
    return dataList;
}

@end
